#include "BaseSocket.h"

#ifdef _WIN32
static CSocketInitializer g_socketInit;
#endif

CBaseSocket::CBaseSocket(void)
:m_hSocket(INVALID_SOCKET)
{
}

CBaseSocket::~CBaseSocket(void)
{
	Close();
}

void CBaseSocket::Close(void)
{
	if (m_hSocket!=INVALID_SOCKET)
	{
		closesocket(m_hSocket);
		m_hSocket=INVALID_SOCKET;
	}
}

void CBaseSocket::Close(int nSock)
{
	if (nSock!=INVALID_SOCKET)
	{
		closesocket(nSock);
		nSock=INVALID_SOCKET;
	}
}

bool CBaseSocket::SetNonBlocking(int nSock)
{
#ifdef _WIN32
	unsigned long lValue = 1;
	int nRet = ioctlsocket(nSock, FIONBIO, &lValue);
	if (nRet != 0)
		return false;

	return true;
#endif
}

bool CBaseSocket::SetNonBlocking(void)
{
	return SetNonBlocking(m_hSocket);
}

void CBaseSocket::SetRcvSndBuff()
{
	SetRcvSndBuff(m_hSocket);
}

void CBaseSocket::SetRcvSndBuff(int nSock)
{
	int sock_buf_size = MAX_SOCK_BUFF_LEN;

	setsockopt( nSock, SOL_SOCKET, SO_SNDBUF,
		(char *)&sock_buf_size, sizeof(sock_buf_size) );

	setsockopt( nSock, SOL_SOCKET, SO_RCVBUF,
		(char *)&sock_buf_size, sizeof(sock_buf_size) );
}
